package longrise.phone.com.bjjt_henan.widget.photo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.android.util.Util;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.widget.photo.bean.ImageFloder;

/**
 * Created by chengsan on 17/6/22.
 */

public class PhotoListAapter extends BaseAdapter {

    private List<ImageFloder> mImageFloders;
    private Context context;

    public PhotoListAapter(Context context) {
        this.context = context;
    }

    public void setmImageFloders(List<ImageFloder> mImageFloders) {
        this.mImageFloders = mImageFloders;
    }

    @Override
    public int getCount() {
        if (null != mImageFloders) {
            return mImageFloders.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (null != mImageFloders) {
            return mImageFloders.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            PhotosItem photosItem = new PhotosItem(context);

            viewHolder.imageView = photosItem.imageView;
            viewHolder.photoName = photosItem.photoName;
            viewHolder.numText = photosItem.numText;

            view = photosItem;
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (null != mImageFloders) {
            ImageFloder imageFloder = mImageFloders.get(i);
            if (null != imageFloder) {
                if (viewHolder.imageView != null) {
                    viewHolder.imageView.setImageBitmap(
                            PhotoListAapter.getBitmapFromSDCardByPath(imageFloder.getFirstImagePath()));
                }
                if (viewHolder.photoName != null) {
                    viewHolder.photoName.setText(imageFloder.getName());
                }
                if (viewHolder.numText != null) {
                    viewHolder.numText.setText(imageFloder.getCount() + "张");
                }
            }
        }

        return view;
    }

    class ViewHolder {
        public ImageView imageView;
        public TextView photoName;
        public TextView numText;
    }

    class PhotosItem extends LinearLayout {

        public ImageView imageView;
        public TextView photoName;
        public TextView numText;


        public PhotosItem(Context context) {
            super(context);
            initUI(context);
        }

        private void initUI(Context context) {
            this.setOrientation(LinearLayout.HORIZONTAL);
            int pad = Util.dip2px(context, 5);
            this.setPadding(pad, pad, pad, pad);
            this.setGravity(Gravity.CENTER_VERTICAL);

            imageView = new ImageView(context);
            imageView.setPadding(Util.dip2px(context, 12),
                    Util.dip2px(context, 9), Util.dip2px(context, 12),
                    Util.dip2px(context, 12));
            imageView.setImageResource(R.drawable.ic_launcher);
            imageView.setBackgroundResource(R.drawable.im_pic_dir);
            this.addView(imageView, Util.dip2px(context, 80), Util.dip2px(context, 80));

            LinearLayout contentLayout = new LinearLayout(context);
            contentLayout.setOrientation(LinearLayout.VERTICAL);
            contentLayout.setGravity(Gravity.CENTER_VERTICAL);
            contentLayout.setPadding(Util.dip2px(context, 15), 0, 0, 0);
            this.addView(contentLayout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            photoName = new TextView(context);
            photoName.setTextSize(16);
            photoName.setText("所有图片");
            photoName.setTextColor(Color.parseColor("#000000"));
            contentLayout.addView(photoName, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            numText = new TextView(context);
            numText.setTextSize(15);
            numText.setText("0张");
            numText.setTextColor(Color.parseColor("#000000"));
            contentLayout.addView(numText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        }
    }

    /**
     * 从SD卡中获取一张图片，做了OOM处理
     *
     * @param localPath 图片本地路径
     * @return
     */
    private static Bitmap getBitmapFromSDCardByPath(String localPath) {
        Bitmap bitmap = null;
        if (null == localPath || localPath.isEmpty()) {
            return bitmap;
        }

        if (!new File(localPath).exists()) {
            return bitmap;
        }

        try {
            //1、加载位图
            FileInputStream fileInputStream = new FileInputStream(localPath);

            //2、为位图设置100K的缓存
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inTempStorage = new byte[100 * 1024];

            //3、设置位图颜色显示优化方式
            /*
                //ALPHA_8：每个像素占用1byte内存（8位）
				//ARGB_4444:每个像素占用2byte内存（16位）
				//ARGB_8888:每个像素占用4byte内存（32位）
				//RGB_565:每个像素占用2byte内存（16位）
				//Android默认的颜色模式为ARGB_8888，这个颜色模式色彩最细腻，显示质量最高。但同样的，占用的内存//也最大。也就意味着一个像素点占用4个字节的内存。我们来做一个简单的计算题：3200*2400*4 bytes //=30M。如此惊人的数字！哪怕生命周期超不过10s，Android也不会答应的。
			 */
            options.inPreferredConfig = Bitmap.Config.RGB_565;

            //4、设置图片在内存不足时可以被回收
            options.inPurgeable = true;

            //5、设置位图的缩放比例
            /*
                width，hight设为原来的四分一（该参数请使用2的整数倍）,这也减小了位图占用的内存大小；
				例如，一张//分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码，产生的Bitmap大小约为//512*384px。
				相较于完整图片占用12M的内存，这种方式只需0.75M内存(假设Bitmap配置为//ARGB_8888)。
			 */
            options.inSampleSize = 4;

            //6、设置解码位图的尺寸信息
            options.inInputShareable = true;

            //7、解码位图
            bitmap = BitmapFactory.decodeStream(fileInputStream, null, options);

            return bitmap;


        } catch (Exception e) {
            e.printStackTrace();
            return bitmap;
        }
    }
}
